#include "GtGui.h"

const bool CGtGui::InitCanvas(::HDC ptrSouDc, RECT & clRect, ::HDC & InitDc, ::HBITMAP & InitBmp, int iBackgroundModes)
{
	bool bRetVal = false;
	try
	{
		InitDc = ::CreateCompatibleDC(ptrSouDc);
		InitBmp = ::CreateCompatibleBitmap(InitDc, clRect.right - clRect.left, clRect.bottom - clRect.top);
		::SelectObject(InitDc, InitBmp);
		::SetBkMode(InitDc, iBackgroundModes);
		bRetVal = true;
	}
	catch (...)
	{

	}
__end:
	return bRetVal;
}

const bool CGtGui::InitFont(::HFONT & clFont, int iHeight, int iWidth, const char * pszFontName)
{	
	clFont = ::CreateFontA(iHeight, iWidth, 0, 0, FW_THIN, 0, 0, 0,	GB2312_CHARSET, 
		                   OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY,
		                   FF_MODERN, pszFontName);
	return clFont != 0;
}

const bool CGtGui::InitBrush(::HBRUSH & clBrush, const COLORREF dwColor)
{
	clBrush = ::CreateSolidBrush(dwColor);
	return clBrush != 0;
}

const bool CGtGui::InitPen(::HPEN & clPen, int iStyle, int iWidth, const COLORREF dwColor)
{
	clPen = ::CreatePen(iStyle, iWidth, dwColor);
	return clPen != 0;
}

void CGtGui::InitRect(::RECT & clRect, int iLeft, int iTop, int iHeight, int iWidth)
{
	clRect.left = iLeft;
	clRect.top = iTop;
	clRect.right = iLeft + iWidth;
	clRect.bottom = iTop + iHeight;
}

const bool CGtGui::FillColor(::HDC & clHdc, ::RECT & refRect, const COLORREF dwColor)
{
	bool bRetVal = false;
	::HBRUSH clBrush, ptrOldBrush;
	try
	{
		if (!InitBrush(clBrush, dwColor)) goto __end;
		ptrOldBrush = (::HBRUSH)SelectObject(clHdc, clBrush);
		::FillRect(clHdc, &refRect, clBrush);
		bRetVal = true;
	}
	catch (...)
	{

	}
	SelectObject(clHdc, ptrOldBrush);
	DeleteObject(clBrush);
__end:
	return bRetVal;
}

const bool CGtGui::FillColor(::HDC & clHdc, int iLeft, int iTop, int iHeight, int iWidth, const COLORREF dwColor)
{
	::RECT clRect;
	InitRect(clRect, iLeft, iTop, iHeight, iWidth);
	return FillColor(clHdc, clRect, dwColor);
}

const bool CGtGui::Rectangle(::HDC & clHdc, ::RECT & refRect, const COLORREF dwBackgroundColor, const COLORREF dwLinColor, int iStyle, int iLinWidth)
{
	bool bRetVal = false;
	::HPEN clPen, clOldPen;
	try
	{
		if (!InitPen(clPen, PS_SOLID, iStyle, dwLinColor)) goto __end;
		if (FillColor(clHdc, refRect, dwBackgroundColor)) goto __end;
		clOldPen = (::HPEN)SelectObject(clHdc, clPen);
		::Rectangle(clHdc, refRect.left, refRect.top, refRect.right, refRect.bottom);
		SelectObject(clHdc, clOldPen);
		bRetVal = true;
	}
	catch (...)
	{

	}
	DeleteObject(clPen);
__end:
	return bRetVal;
}

const bool CGtGui::OutText(::HDC & clHdc, ::RECT & refRect, ::HFONT & clFont, const char * pszText, const COLORREF dwFontColor, const COLORREF dwBackgroundColor, CAlign emAlign)
{
	bool bRetVal = false;
	COLORREF wdOldFontColor = ::SetTextColor(clHdc, dwFontColor);
	HFONT clOldFont = (HFONT)SelectObject(clHdc, clFont);
	if (!(bRetVal = FillColor(clHdc, refRect, dwBackgroundColor))) goto __end;
	switch (emAlign)
	{
	case 0: DrawTextA(clHdc, pszText, strlen(pszText), &refRect, DT_LEFT);
	case 1: DrawTextA(clHdc, pszText, strlen(pszText), &refRect, DT_CENTER || DT_SINGLELINE || DT_VCENTER);
	case 2: DrawTextA(clHdc, pszText, strlen(pszText), &refRect, DT_RIGHT);
	}

__end:
	::SetTextColor(clHdc, wdOldFontColor);
	::SelectObject(clHdc, clOldFont);
	DeleteObject(clFont);
	return bRetVal;
}
